package list

import "github.com/therecipe/qt/core"

type SongData struct{
	Title string `json:"name"`
	Artist string `json:"alias"`
	Album string `json:"album__name"`
	Url string	`json:"mp3_url"`
}

const (
	SongData__TitleRole = 0
	SongData__ArtistRole = 1
	SongData__AlbumRole = 2
	SongData__UrlRole = 3
)

type SongModel struct{
	core.QAbstractListModel
	songDatas []*SongData
	_ func() `constructor:"init"`
}

func (this *SongModel) rowCount(parent *core.QModelIndex) int {
	return len(this.songDatas)
}

func (this *SongModel) data(index *core.QModelIndex, role int) *core.QVariant {
	if index.IsValid() || index.Row() == 0 && this.rowCount(index) > 0{
		return core.NewQVariant()
	}
	row := index.Row()
	item := this.songDatas[row]
	switch(role){
	case SongData__TitleRole:return core.NewQVariant14(item.Title)
	case SongData__ArtistRole:return core.NewQVariant14(item.Artist)
	case SongData__AlbumRole:return core.NewQVariant14(item.Album)
	case SongData__UrlRole:return core.NewQVariant14(item.Url)
	}

	return core.NewQVariant()
}

func (this *SongModel) SetThisValue (songDatas []*SongData){
	this.BeginResetModel()
	this.songDatas = songDatas
	this.EndResetModel()
}

func (songModel *SongModel) init(){
	songModel.ConnectRowCount(songModel.rowCount)
	songModel.ConnectData(songModel.data)
}